package org.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.mapper.UserMapper;
import org.example.domain.User;
import org.example.service.UserService;
import org.example.util.PasswordUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    @Override
    public List<User> selectAllUser(Integer pageNum, Integer pageSize, String searchQuery) {
        // 创建分页对象
        Page<User> page = new Page<>(pageNum, pageSize);
        
        // 创建查询条件
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        
        // 如果有搜索关键词，添加用户名模糊查询条件
        if (StringUtils.hasText(searchQuery)) {
            queryWrapper.like(User::getUsername, searchQuery);
        }
        
        // 执行分页查询
        Page<User> userPage = userMapper.selectPage(page, queryWrapper);
        
        // 返回当前页的数据
        return userPage.getRecords();
    }

    @Override
    public long getTotalCount(String searchQuery) {
        // 创建查询条件
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        
        // 如果有搜索关键词，添加用户名模糊查询条件
        if (StringUtils.hasText(searchQuery)) {
            queryWrapper.like(User::getUsername, searchQuery);
        }
        
        // 返回总记录数
        return userMapper.selectCount(queryWrapper);
    }
    
    @Override
    public long getActiveUserCount() {
        // 创建查询条件
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        
        // 获取24小时前的时间
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.HOUR, -24);
        Date oneDayAgo = calendar.getTime();
        
        // 添加最后登录时间大于24小时前的条件
        queryWrapper.ge(User::getLastLoginTime, oneDayAgo);
        
        // 返回活跃用户数量
        return userMapper.selectCount(queryWrapper);
    }
    
    @Override
    public long getNewUserCount() {
        // 创建查询条件
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        
        // 获取24小时前的时间
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.HOUR, -24);
        Date oneDayAgo = calendar.getTime();
        
        // 添加创建时间大于24小时前的条件
        queryWrapper.ge(User::getCreateTime, oneDayAgo);
        
        // 返回新增用户数量
        return userMapper.selectCount(queryWrapper);
    }

    @Override
    public String getUserPermissions(Long userId) {
        return userMapper.selectUserPermissions(userId);
    }

    @Override
    public boolean existsByUsername(String username) {
        HashMap<String, Object> findMap = new HashMap<>();
        findMap.put("username",username);
        List<User> users = userMapper.selectByMap(findMap);
        if (users.size()==0){
            return false;
        }
        return true;
    }

    @Override
    public User save(User user) {

        // 设置默认角色为普通用户
        user.setRole("user");
        user.setCreateTime(new Date());
        user.setLastLoginTime(new Date());
        user.setPassword(PasswordUtils.encode(user.getPassword()));
        userMapper.insert(user);
        return user;
    }

    @Override
    public int update(User user) {
        return userMapper.updateById(user);
    }
    /***
     * 新增
     * @param user
     * @return
     */
    @Override
    public int add(User user) {
        user.setCreateTime(new Date());
        user.setPassword(PasswordUtils.encode(user.getPassword()));
        return userMapper.insert(user);
    }

    /***
     * 获取当前用户
     * @return
     */
    @Override
    public User getCurrentUser(Long userId) {
        return userMapper.getCurrentUser(userId);
    }
}
